/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gui;

import javax.microedition.lcdui.*;
import java.util.Vector;
import ral.Real;
import parser.CargaXML;
import gui.MenuCines;
import javax.microedition.lcdui.*;
import org.kxml.kdom.*;
import app.ConsultaCartelera;

/**
 * @author REX
 */
public class MenuCines extends Canvas implements CommandListener {
    Vector listaCines = new Vector(5,2);
    private int width,height;
    private int anchor = Graphics.TOP | Graphics.LEFT;
    private MenuPrincipal mp;
    private Mapa m;
    private MenuCartelera cm;
    private int visibles = 3;
    private int barTitulo = 20;
    private int altoVisible;
    private int scrollY;
    private FuncionalidadMenuCines fmc;

    public MenuCines(MenuPrincipal mp) {
        try {
            width = getWidth();
            height = getHeight();
	    setCommandListener(this);
	    addCommand(new Command("Mapa", Command.SCREEN, 1));
            addCommand(new Command("Cartelera", Command.SCREEN,2));
            
            obtenListaDeCines();
            this.mp = mp;
            altoVisible =  height - barTitulo ;
            scrollY =  barTitulo;
            fmc = new FuncionalidadMenuCines(0,listaCines.size() - 1,0,scrollY,visibles);
            
        } catch(Exception e) {
            e.printStackTrace();
        }
    } 
    
    /**
     * paint
     */
    public void paint(Graphics g) {
        limpiaPantalla(g);
        Font f = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_LARGE);
        g.setFont(f);
        g.setColor(0x32639A);
        /*Titulo*/
        String titulo = "Lista De Cines";
        int anchoTitulo = f.stringWidth(titulo);
        int altoTitulo = f.getHeight();
        int posXt = (width - anchoTitulo)/2;
        int posYt = barTitulo - altoTitulo;
        g.drawString(titulo,posXt ,posYt , anchor);
        g.setColor(0x4486C7);
        g.setStrokeStyle(Graphics.SOLID);
        g.drawLine(0, barTitulo - 1, width, barTitulo -1);
        g.drawLine(0, barTitulo, width, barTitulo);
       
        /*
         Tamaño de los cuadros de las opciones
         */
        Font f2 = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_MEDIUM);
        g.setFont(f2);
      
        int nW =(int)(width);
        int nH = ((int)(altoVisible)   )/visibles;
        int deltax = (int)(width*0.00);
        int deltay = barTitulo + 1;
        int y;
        /*Scroll bar*/
      /*  int sbbW = (int)(width*0.05);
        int sbbH = (int)altoVisible;
        g.setColor(0xCCCCFF);
        g.fillRect(nW, barTitulo, sbbW, sbbH);
        
        int sbW = (int)(width*0.05);
        int sbH = (int)(altoVisible/(listaCines.size()));
        fmc.setScrollHeight(sbH);*/
        /************/
        int k = 0;
        for(int i=fmc.getInicial();i<=fmc.getFinal();i++){
            y = deltay +  (nH)*k;
            int delayf = deltax;
            if(fmc.getActual() == i)
                dibujaSeleccionado( g, deltax, delayf, y, nW, nH,   i);
            else
                dibujaNoSeleccionado( g, deltax, delayf, y, nW, nH,  i);
        
            k++;

        }
    }
    public void dibujaScroll(Graphics g, int x, int y, int w,int h){
        g.setColor(0x9900CC);
        g.fillRect(x, y, w, h);
    }
    public void dibujaSeleccionado(Graphics g,int deltax,int delayf,int y,int nW,int nH,int  i){
        Cine c = (Cine)listaCines.elementAt(i);
        Font f2 = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_MEDIUM);
        g.setFont(f2);
        g.setColor(0xE0FFFF);
        g.fillRect( deltax, y , nW, nH );
        g.setColor(0x5D781D);
        g.drawString(c.getNombre(), delayf, y , anchor);
        g.setColor(0xA8A8A8 );
        Font f3 = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_SMALL);
        g.setFont(f3);

        //******** Latitud y longitud del cine actual  *******
        Element a  =CargaXML.obtenRoot("http://www.ngwise.com/cm/cines.php");
        Element cine=a.getElement(i);
        Real latitud= new Real(cine.getElement("latitud").getText());
        Real longitud=new Real(cine.getElement("longitud").getText());

       //******* Metodo Para la Distancia
        g.drawString
                ("Distancia " + c.calculaDistncia(latitud,longitud).toString(),delayf,y + nH/2,anchor);

        g.setColor(0x89B056);
        g.drawRect( deltax, y, nW, nH);
    }
    public void dibujaNoSeleccionado(Graphics g,int deltax,int delayf,int y,int nW,int nH,int  i){
        Cine c = (Cine)listaCines.elementAt(i);
        Font f2 = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_MEDIUM);
        g.setFont(f2);
        g.setColor(0xFFFFFF);
        g.fillRect( deltax, y , nW, nH );
        g.setColor(0x5D781D);
        g.drawString(c.getNombre(), delayf, y , anchor);
        g.setColor(0xA8A8A8 );
        Font f3 = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_SMALL);
        g.setFont(f3);

        //******** Latitud y longitud del cine actual  *******
        Element a  =CargaXML.obtenRoot("http://www.ngwise.com/cm/cines.php");
        Element cine=a.getElement(i);
        int id=Integer.parseInt(cine.getElement("id").getText());
        Real latitud= new Real(cine.getElement("latitud").getText());
        Real longitud=new Real(cine.getElement("longitud").getText());
        //***** Calcula La distancia***
        g.drawString("Distancia " + c.calculaDistncia(latitud,longitud).toString(),delayf,y + nH/2,anchor);
        g.setColor(0x89B056);
        g.drawRect( deltax, y, nW, nH);
    }
    public void limpiaPantalla(Graphics g){
        g.setColor( 255, 255, 255 );
        g.fillRect( 0, 0, width, height );
    }
    public void obtenListaDeCines(){

        listaCines.removeAllElements();
        Element c=CargaXML.obtenRoot("http://www.ngwise.com/cm/cines.php");
        for(int i=0;i<c.getChildCount();i++){
            Element cine=c.getElement(i);
            int id=Integer.parseInt(cine.getElement("id").getText());
            String nombre=cine.getElement("nombre").getText();
            String latitud=cine.getElement("latitud").getText();
            String longitud=cine.getElement("longitud").getText();
            listaCines.addElement(new Cine(id,nombre,new Real(latitud),new Real (longitud)));
        }

       
    }
    /**
     * Called when a key is pressed.
     */
    protected  void keyPressed(int keyCode) {
         String str = getKeyName(keyCode);
        if (str.equals("Up") || str.equals("UP")) {
            fmc.subir();
            repaint();
        }if (str.equals("Down") || str.equals("DOWN")) {
            fmc.bajar();
            repaint();
        }if(str.equals("Right") || str.equals("RIGHT") || str.equals("Fire") || str.equals("FIRE")){


        }if(str.equals("Left") || str.equals("LEFT")){
            mp.getMidlet().pantalla.setCurrent(mp);
        }
    }

    public void commandAction(Command c, Displayable displayable) {
         Cine cineActual =(Cine) listaCines.elementAt( fmc.getActual() );
         if (c.getLabel().equals("Mapa")) {
            if(m == null){
                m = new Mapa(cineActual, this);
            }else{
                m.setCine( cineActual );
            }
            mp.getMidlet().pantalla.setCurrent(m);
         }
         if (c.getLabel().equals("Cartelera")) {
            if(cm == null){
                cm = new MenuCartelera(cineActual, this);
            }else{
                cm.setCine(cineActual);
            }
            mp.getMidlet().pantalla.setCurrent(cm);
         }
    }

    public MenuPrincipal getMp() {
        return mp;
    }

    public void setMp(MenuPrincipal mp) {
        this.mp = mp;
    }

}
